package lesson01

import (
	"database/sql"
	"gorm.io/gorm"
	"time"
)

type User struct {
	Id           uint
	Name         string
	Email        *string
	Age          uint8
	Birthday     *time.Time
	MemberNumber sql.NullString
}

type Member struct {
	gorm.Model
	Name string
	Age  uint8
}

type Author struct {
	Name  string
	Email string
}

type Blog struct {
	Author
	ID      int
	Upvotes int32
}

type Blog2 struct {
	ID     int64
	Author Author `gorm:"embedded;embeddedPrefix:author_"`
	// Author  Author
	Upvotes int32
}

func Run(db *gorm.DB) {
	//db.AutoMigrate(&User{})
	/*email := "zhangsan@163.com"
	// 先将时间存储到变量中
	birthday := time.Now()
	user := User{
		Name:     "张三",
		Email:    &email,
		Age:      18,
		Birthday: &birthday,
		MemberNumber: sql.NullString{
			String: "123456",
			Valid:  true,
		},
	}

	db.Create(&user)
	fmt.Println(user.Id)*/

	// 查询Id为1的user记录，并打印出
	/*var user1 User
	db.First(&user1, 1)
	fmt.Println(user1)*/

	// 更新Id为1的user的name为李四
	//db.Model(&User{}).Where("id = ?", 1).Update("name", "李四1")
	// 删除Id为1的user记录
	//db.Delete(&User{}, 2)
	//db.Where("id = ?", 1).Delete(&User{})

	/*// 初始化表
	db.AutoMigrate(&Member{})
	// 创建Member
	mem := Member{Name: "张三", Age: 18}
	db.Create(&mem)
	fmt.Println(mem.ID)*/

	db.AutoMigrate(&Blog{}, &Blog2{})

}
